//
//  CreaditUtilsHelper.m
//

#import "CreaditUtilsHelper.h"


@implementation CreaditUtilsHelper

+ (void)boxShowErrorWithMessage:(NSString *)message duration:(NSTimeInterval)duration{
    [self boxShowWithMessage:message duration:duration];
}

+ (void)showMsg:(NSString *)message withImgName:(NSString *)imgName duration:(NSTimeInterval)duration{
    
    CGFloat imgWidth = 40;
    CGFloat boxSizeW = 100;
    CGFloat boxSizeH = 100;
    
    if (PMStringIsEmpty(message)) {
        boxSizeW = 70;
        boxSizeH = 70;
    }
    else{
        boxSizeW = 100;
        boxSizeH = 100;
    }
    
    CGSize size = CGSizeZero;
    if (PMStringIsEmpty(message)) {
        size = CGSizeMake(60, 60);
    }
    else{
        size = [message boundingRectWithSize:CGSizeMake(kScreenWidth - 100, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    }
    
    UIView * boxView = [[UIView alloc] init];
    boxView.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    boxView.alpha = 0.75;
    
    boxView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    
    boxView.layer.cornerRadius = 8;
    boxView.layer.masksToBounds = YES;
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    
    [window addSubview:boxView];

    UIImageView *imgV = [[UIImageView alloc] init];
    [boxView addSubview:imgV];
    imgV.image = [UIImage imageNamed:imgName];
        
    UILabel *lab  = [Utility createLabel:[UIFont systemFontOfSize:15] color:[UIColor whiteColor]];
    lab.text = esString(message);
    lab.numberOfLines = 0;
    lab.textAlignment = 1;
    [boxView addSubview:lab];
    
    if (size.width < 100 && size.height < 100) {
        boxSizeW = 100;
        boxSizeH = 100;
    }
    else{
        boxSizeW = size.width + 20;
        boxSizeH = 15 + imgWidth + 10 + size.height + 20;
    }
    
    boxView.frame = CGRectMake((kScreenWidth - boxSizeW)/2, (kScreenHeight  - boxSizeH) / 2, boxSizeW, boxSizeH);
    
    if (PMStringIsEmpty(message)) {
        [imgV setFrame:CGRectMake((boxSizeW - imgWidth) / 2, (boxSizeH - imgWidth)/2, imgWidth, imgWidth)];
    }
    else{
        [imgV setFrame:CGRectMake((boxSizeW - imgWidth) / 2, 15, imgWidth, imgWidth)];
        lab.frame = CGRectMake((boxSizeW - size.width)/2, imgV.bottom + 10, size.width, size.height);
    }
    
    [UIView animateWithDuration:duration animations:^{
        boxView.alpha = 0;
    } completion:^(BOOL finished) {
        [boxView removeFromSuperview];
    }];
    
}

+ (void)boxShowWithMessage:(NSString *)message onView:(UIView *)view withImg:(UIImage *)img duration:(NSTimeInterval)duration {
    
    CGFloat imgW = 0;
    CGFloat widthText = 290 - 24;
    if (img) {
        imgW = 20;
        widthText = 290 - 12 - imgW - 10 - 12;
    }
    
    
    CGSize size = CGSizeZero;
    if (PMStringIsEmpty(message)) {
        size = CGSizeMake(60, 60);
    }
    else{
        size = [message boundingRectWithSize:CGSizeMake(widthText, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    }
    CGFloat backWidth = size.width + 24;
    if (img) {
        backWidth = size.width + 24 + imgW + 10;
    }
    
    
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backWidth, size.height + 24)];
    backView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.85f];
    backView.layer.cornerRadius = 8;
    backView.layer.masksToBounds = YES;
    backView.center = CGPointMake(view.bounds.size.width * 0.5, view.bounds.size.height * 0.5);
    [view addSubview:backView];
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(12, (backView.frame.size.height - imgW)/2, imgW, imgW)];
    imgV.image = img;
    
    if (img) {
        [backView addSubview:imgV];
    }
    
    
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(img?imgV.right + 10:12, 12, size.width, size.height)];
    messageLabel.font = [UIFont systemFontOfSize:15];
    messageLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    messageLabel.text = esString(message);
    messageLabel.numberOfLines = 0;
    messageLabel.textAlignment = 1;
    [backView addSubview:messageLabel];
   
    [UIView animateWithDuration:duration animations:^{
        backView.alpha = 0;
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
    }];
}

+ (void)boxShowWithMessage:(NSString *)message onView:(UIView *)view duration:(NSTimeInterval)duration {
    
    CGSize size = CGSizeZero;
    if (PMStringIsEmpty(message)) {
        size = CGSizeMake(60, 60);
    }
    else{
        size = [message boundingRectWithSize:CGSizeMake(view.bounds.size.width - 80, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
    }
   
    CGFloat width = size.width + 40;
    CGFloat height = size.height + 30;
    
    UIView * textView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textView.backgroundColor = [UIColor blackColor];
    textView.alpha = 1;
    textView.layer.cornerRadius = 10;
    textView.layer.masksToBounds = YES;
    textView.center = CGPointMake(view.bounds.size.width * 0.5, view.bounds.size.height * 0.5);
    
    UILabel * messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, size.width, size.height)];
    messageLabel.font = [UIFont systemFontOfSize:16];
    messageLabel.textColor = [UIColor whiteColor];
    messageLabel.text = esString(message);
    messageLabel.numberOfLines = 0;
    [textView addSubview:messageLabel];
    [view addSubview:textView];
    messageLabel.textAlignment = 1;
    
    [UIView animateWithDuration:duration animations:^{
        textView.alpha = 0;
    } completion:^(BOOL finished) {
        [textView removeFromSuperview];
    }];
}

+ (void)boxShowWithMessage:(NSString *)message duration:(NSTimeInterval)duration{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    [self boxShowWithMessage:message onView:window withImg:nil duration:duration];
}

+ (void)boxShowWithMessage:(NSString *)message onView:(UIView *)view{
    [self boxShowWithMessage:message onView:view withImg:nil duration:2.5f];
}

+ (void)boxShowWithMessage:(NSString *)message{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    [self boxShowWithMessage:message onView:window withImg:nil duration:2.5];
}

+ (void)boxShowLoadWithMessage:(NSString *)message onView:(UIView *)view withOffsetHeight:(CGFloat)offHeight{
    
    UIView *shadeV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    shadeV.backgroundColor = [UIColor clearColor];
    shadeV.tag = 70000;
    [view addSubview:shadeV];
    
    
    CGFloat indicatorWidth = 40;
    CGFloat waitingIconSize = 100;
    if (PMStringIsEmpty(message)) {
        waitingIconSize = 70;
    }
    else{
        waitingIconSize = 100;
    }
    
    UIView *boxView = [[UIView alloc] init];
    boxView.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    boxView.alpha = 0.85;
    

    boxView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    

    boxView.frame = CGRectMake((kScreenWidth - waitingIconSize)/2, (kScreenHeight - waitingIconSize) / 2 + offHeight, waitingIconSize, waitingIconSize);
    
    boxView.layer.cornerRadius = 8;
    boxView.layer.masksToBounds = YES;
    
    [shadeV addSubview:boxView];
    
    
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [indicatorView startAnimating];
    [boxView addSubview:indicatorView];
    
    UILabel *lab  = [Utility createLabel:[UIFont systemFontOfSize:15] color:[UIColor whiteColor]];
    lab.text = esString(message);
    lab.textAlignment = 1;
    [boxView addSubview:lab];
    
    if (PMStringIsEmpty(message)) {
        [indicatorView setFrame:CGRectMake((waitingIconSize - indicatorWidth) / 2, (waitingIconSize - indicatorWidth)/2, indicatorWidth, indicatorWidth)];
    }
    else{
        [indicatorView setFrame:CGRectMake((waitingIconSize - indicatorWidth) / 2, 15, indicatorWidth, indicatorWidth)];
        lab.frame = CGRectMake(0, indicatorView.bottom + 10, waitingIconSize, 20);
    }
    
}

+ (void)boxShowLoadWithMessage:(NSString *)message{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    [self boxShowLoadWithMessage:message onView:window withOffsetHeight:0];
}

+ (void)boxShowLoadWithMessage:(NSString *)message onView:(UIView *)view{
    [self boxShowLoadWithMessage:message onView:view withOffsetHeight:-kNavigationBarHeight];
}

+ (void)hideLoadMessageBoxFromView:(UIView *)view{
    UIView * boxView = [view viewWithTag:70000];
    
    [UIView animateWithDuration:1.5f animations:^{

    } completion:^(BOOL finished) {
        [boxView removeFromSuperview];
    }];
    
}

+ (void)hideLoadMessageBox{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    [self hideLoadMessageBoxFromView:window];
}

+ (void)actionWhenLogin:(VoidBlock)block onVC:(UIViewController *)vc {
    
    if ([[CurrentUser mine] hasLogged]) {
        if (block) {
            block();
        }
    } else {
        [[CreditCore globleCore] loginController:^(void){
            if (block) {
                block();
            }
            
        }];
    }
}

+ (void)actionWhenLogin:(VoidBlock)block{
    
    [self actionWhenLogin:block onVC:nil];

}



@end
